﻿package framework.layer 
{
	import com.Trim;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import framework.data.ParseTxtData;
	import framework.util.mc.MCUtil;

	/**
	 * 层管理器
	 * @author henrylee
	 */
	public class LayerManager 
	{
		
		// 层LabelName
		public static const BG_LAYER:String    = "bgLayer";
		public static const GAME_LAYER:String  = "gameLayer";
		public static const TOP_UI:String      = "topUI";
		public static const TEXT_TIPS:String   = "textTipsLayer";
		
		
		// 组合索引
		public static const HALL_GROUP: int = 0;
		public static const GAME_GROUP: int = 1;
		
		////////////////////////////////////////////////////////////////////////////////
		
		private var currentGroupIndex: int = -1;
		
		private var _stage: Stage;
		
		private var rawData: String;
		
		private var layerLabels: Vector.<LayerLabel>;
		
		private var layerGroup: Vector.<Vector.<String>>;
		
		private var layerDic: Dictionary;
		
		/**
		 * 构造
		 */
		public function LayerManager(single:Single)
		{
			layerLabels = new Vector.<LayerLabel>();
			layerGroup = new Vector.<Vector.<String>>();
			layerDic = new Dictionary();
		}
		
		/**
		 * 初始化
		 */
		public function init(stage: Stage, data: String):void
		{
			_stage = stage;
			rawData = data;
			parseConfig();
			createLayers();
			rebuildGroup(HALL_GROUP);
			
			var rect: Rectangle = new Rectangle(0, 0, _stage.stageWidth, _stage.height);
			getLayer(BG_LAYER).scrollRect   = rect;
			getLayer(GAME_LAYER).scrollRect = rect;
			getLayer(TOP_UI).scrollRect     = rect;
		}
		
		private function parseConfig():void
		{
			var tmp: Array = ParseTxtData.parse(rawData);
			var len: int = tmp.length;
			var i: int = 0;
			var tmpStr: String;
			while (i < len)
			{
				tmpStr = tmp[i];
				if (tmpStr.length > 3 && tmpStr.indexOf(",") != -1 && tmpStr.indexOf("/") == -1)
				{
					if (tmpStr.indexOf("$") != -1)
						layerGroup.push(parseGroup(tmpStr));
					else
						layerLabels.push(parseLabel(tmpStr));
				}
				i++;
			}
		}
		
		
		private function parseGroup(str: String):Vector.<String>
		{
			var vec: Vector.<String> = new Vector.<String>();
			var tmpStr: String = Trim.trim(str.substr(str.indexOf("$") + 1));
			var tmpArr: Array = tmpStr.split(",");
			for each(var tag: String in tmpArr)
			{
				vec.push(Trim.trim(tag));
			}
			return vec;
		}
		
		
		private function parseLabel(str: String):LayerLabel
		{
			var label: LayerLabel = new LayerLabel();
			var tmpArr: Array = str.split(",");
			label.name = Trim.trim(tmpArr[0]);
			label.index = int(tmpArr[1]);
			label.mouseEnable = (int(tmpArr[2]) == 1);
			return label;
		}
		
		
		private function createLayers():void
		{
			var len: int = layerLabels.length;
			var label: LayerLabel;
			while (len--)
			{
				label = layerLabels[len];
				var sprite:Sprite = new Sprite();
				layerDic[label.name] = [sprite, label.index];
				
				if (!label.mouseEnable)
				{
					sprite.mouseChildren = false;
					sprite.mouseEnabled = false;
				}
			}
		}
		
		
		/**
		 * 清理所有已添加显示对象
		 */
		public function clearAll():void
		{
			if (currentGroupIndex == -1) return;
			var layer: Sprite;
			var groups: Vector.<String> = layerGroup[currentGroupIndex];
			var len: int = groups.length;
			while (len--)
			{
				clearLayers(groups[len]);
				layer = layerDic[groups[len]][0];
				layer.parent.removeChild(layer);
			}
		}
		
		
		/**
		 * 清理某一层的所有显示对象
		 * @param	type
		 */
		public function clearLayers(type:String):void
		{
			MCUtil.removeAllChild(getLayer(type));
		}
		
		
		/**
		 * 为不同的场景提供不同的层级组合
		 * @param	index
		 */
		public function rebuildGroup(index:int = HALL_GROUP):void
		{
			if (index == -1 || index >= layerGroup.length) return;
			clearAll();
			
			currentGroupIndex = index;
			var groups: Vector.<String> = layerGroup[currentGroupIndex];
			var len: int = groups.length;
			var i: int;
			while (i<len)
			{
				_stage.addChildAt(layerDic[groups[i]][0], layerDic[groups[i]][1]);
				i++;
			}
		}
		
		
		/**
		 * 增加对象到层
		 * @param	type 层的类型属性
		 * @param	displayObj	需要增加的对象s
		 */
		public function addToLayer(type:String , displayObj:DisplayObject):void
		{
			var sprite:Sprite = layerDic[type][0];
			sprite.addChild(displayObj);
		}
		
		/**
		 * 获取层次
		 * @param	type
		 * @return
		 */
		public function getLayer(type:String):Sprite
		{
			var sprite:Sprite = layerDic[type][0];
			return sprite;
		}
		
		/**
		 * 单例引用
		 */
		public static function get instance():LayerManager
		{
			if(_instance == null)
			{
				_instance = new LayerManager(new Single());
			}
			return _instance;
		}
		
		private static var _instance:LayerManager = null;
	}
}
class Single{}